package com.doeis.manage.service;

import java.util.ArrayList;
import java.util.List;

import com.doeis.core.db.DBException;
import com.doeis.core.db.DBManager;
import com.doeis.core.db.DaoException;
import com.doeis.manage.bean.Catalog;
import com.doeis.manage.dao.CatalogDao;
import com.doeis.tools.StringHelper;

public class CatalogService {
	private static final CatalogDao dao = new CatalogDao();

	public List<Catalog> list(int item_id, String keyword) throws Exception {
		try {
			String sql = "select * from " + Catalog.tableName + " where item_id=? ";
			List<Object> params = new ArrayList<Object>();
			params.add(item_id);
			if (StringHelper.isNotNull(keyword)) {
				sql += " and cat_name like ? ";
				params.add("%" + keyword + "%");
			}
			return dao.query(Catalog.class, sql, false, params.toArray());
		} catch (DBException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public Catalog findById(int id) throws Exception {
		try {
			return dao.readById(Catalog.class, id);
		} catch (DaoException e) {
			throw new Exception(e);
		}
	}

	public void add(Catalog catalog) throws Exception {
		try {
			dao.insert(catalog);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public void update(Catalog catalog) throws Exception {
		try {
			dao.update(catalog);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public boolean delete(int id) throws Exception {
		try {
			return dao.delete(Catalog.class, id);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}
}
